home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / text / proged1.lha / InstallProgED / sources / PED_APIDocks / PED_Docks.c next >
C/C++ Source or Header  |  1995-09-07  |  9KB  |  421 lines

  1.  
  2. /******
  3.  * Include vari
  4.  ******/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <libraries/toolmanager.h>
  10. #include <proto/toolmanager.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14. #include <ProgED:sources/include/Ped.h>
  15.  
  16. #define NUMDOCKS    20
  17.  
  18.  
  19.  
  20. /******
  21.  * Strutture e variabili globali
  22.  ******/
  23.  
  24. struct tmTool
  25. {
  26.     APTR    exec,
  27.         icon,
  28.         sound;
  29. };
  30.  
  31. struct APIClient    MyClient=
  32. {
  33.     NULL,            /* Questo campo sara' riempito dopo... */
  34.     NOTIFY_ON_SHOW_HIDE,    /* Dimmi quando chiudi e riapri il tuo schermo */
  35.     "Cliente DOCK",        /* Nome del cliente (non utilizzato ancora) */
  36.     NULL            /* Porre a NULL e non manipolare! Grazie. */
  37. };
  38.  
  39. struct MsgPort        *MyPort,
  40.             *PEDPort;
  41. struct APIMessage     msg;
  42. ULONG             NumDock=0;
  43. UWORD             PunDock[NUMDOCKS],
  44.              DockX[NUMDOCKS],
  45.              DockY[NUMDOCKS];
  46.  
  47. UBYTE             quitting=FALSE;
  48.  
  49.  
  50.  
  51. /*****
  52.  *
  53.  * FUNZIONE:    void SendMessageToPED(struct APIMessage    *msg)
  54.  *
  55.  * SCOPO:    Questa funzione spedisce un messaggio alla porta APi del
  56.  *        ProgED.
  57.  *
  58.  * RESTITUISCE: -
  59.  *
  60.  ****/
  61.  
  62. void SendMessageToPED(struct APIMessage    *msg)
  63. {
  64.     msg->am_Message.mn_Node.ln_Succ=NULL;
  65.     msg->am_Message.mn_Node.ln_Pred=NULL;
  66.     msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
  67.     msg->am_Message.mn_Node.ln_Pri=0;
  68.     msg->am_Message.mn_Node.ln_Name=NULL;
  69.     msg->am_Message.mn_ReplyPort=MyPort;
  70.     msg->am_Message.mn_Length=sizeof(struct APIMessage);
  71.  
  72.     PutMsg(PEDPort,(struct Message *)msg);
  73.     WaitPort(MyPort);
  74.     while(GetMsg(MyPort));
  75. }
  76.  
  77.  
  78.  
  79. /*****
  80.  *
  81.  * FUNZIONE:    struct TagItem *SetupDock(char *config,void *handle)
  82.  *
  83.  * SCOPO:    Crea il dock dato il nome del file di configurazione, l'handle
  84.  *        per il toolmanager e due puntatori che restituiranno la
  85.  *        posizione x ed y del dock.
  86.  *
  87.  * RESTITUISCE: Puntatore al vettore di definizione per creare il dock.
  88.  *
  89.  ****/
  90.  
  91. struct TagItem *SetupDock(char *config,void *handle)
  92. {
  93.     BPTR         file;
  94.     ULONG         num=0,
  95.              oldnum=0,
  96.              totnum=0,
  97.              n=0,
  98.              orien=0,
  99.              col=1,
  100.              i;
  101.     struct TagItem    *dock=NULL;
  102.     struct RDArgs    *RDArgs;
  103.     char         buffer[300],
  104.              execName[10],
  105.              imageName[10];
  106.  
  107.  
  108.  
  109.     if (file=Open(config,MODE_OLDFILE))
  110.     {
  111.         while(FGets(file,buffer,sizeof(buffer)-1))
  112.         {
  113.             if (!strncmp(buffer,"ADD",3))        totnum++;
  114.             else if (!strncmp(buffer,"DOCK",4))    totnum+=10;
  115.         }
  116.  
  117.         Close(file);
  118.  
  119.         if (dock=(struct TagItem *)calloc(totnum*sizeof(struct TagItem),1))
  120.         {
  121.             if (file=Open(config,MODE_OLDFILE))
  122.             {
  123.                 if (RDArgs=AllocDosObject(DOS_RDARGS,NULL))
  124.                 {
  125.                     while(FGets(file,buffer,sizeof(buffer)-1))
  126.                     {
  127.                         ULONG    args[10]={0,0,0,0,0,0,0,0,0,0};
  128.  
  129.  
  130.  
  131.                         RDArgs->RDA_Source.CS_Buffer=buffer+4;
  132.                         RDArgs->RDA_Source.CS_Length=strlen(buffer+4);
  133.                         RDArgs->RDA_Source.CS_CurChr=0;
  134.                         RDArgs->RDA_DAList=NULL;
  135.                         RDArgs->RDA_Buffer=NULL;
  136.  
  137.                         if (!strncmp(buffer,"ADD",3))
  138.                         {
  139.                             if (ReadArgs("COMMAND/K/A,AREXX/S,SHELL/S,DIR/K,OUTPUT/K,IMAGE/K/A",args,RDArgs))
  140.                             {
  141.                                 struct TagItem imageTags[]={
  142.                                     TMOP_File,    NULL,
  143.                                     TMOP_Data,    NULL,
  144.                                 TAG_END};
  145.  
  146.                                 struct TagItem execTags[]={
  147.                                     TMOP_Command,        NULL,
  148.                                     TMOP_ExecType,        NULL,
  149.                                     TMOP_Stack,        10000,
  150.                                     TMOP_CurrentDir,    NULL,
  151.                                     TMOP_Output,        NULL,
  152.                                 TAG_END};
  153.  
  154.                                 imageTags[0].ti_Data=(ULONG)strdup((char *)args[5]);
  155.  
  156.                                 execTags[0].ti_Data=(ULONG)strdup((char *)args[0]);
  157.                                 execTags[1].ti_Data=args[1] ? TMET_ARexx : TMET_CLI;
  158.                                 execTags[3].ti_Data=args[3] ? (ULONG)strdup((char *)args[3]) : NULL;
  159.                                 execTags[4].ti_Data=args[4] ? (ULONG)strdup((char *)args[4]) : NULL;
  160.  
  161.                                 sprintf(execName,"PED_E%d",num);
  162.                                 sprintf(imageName,"PED_I%d",num++);
  163.  
  164.                                 CreateTMObjectTagList(handle,strdup(execName),TMOBJTYPE_EXEC,execTags);
  165.                                 CreateTMObjectTagList(handle,strdup(imageName),TMOBJTYPE_IMAGE,imageTags);
  166.  
  167.                                 FreeArgs(RDArgs);
  168.                             }
  169.                         }
  170.                         else if (!strncmp(buffer,"DOCK",4))
  171.                         {
  172.                             if (ReadArgs("X/N,Y/N,HORIZONTAL/S,COLUMNS/N",args,RDArgs))
  173.                             {
  174.                                 if (args[0])    DockX[NumDock]=*((ULONG *)args[0]);
  175.                                 if (args[1])    DockY[NumDock]=*((ULONG *)args[1]);
  176.                                 if (args[2])    orien=1;
  177.                                 if (args[3])    col=*((ULONG *)args[3]);
  178.  
  179.                                         if (num-oldnum>0)
  180.                                 {
  181.                                     PunDock[NumDock++]=n;
  182.  
  183.                                     dock[n].ti_Tag=TMOP_PubScreen;
  184.                                     dock[n++].ti_Data=NULL;
  185.  
  186.                                     dock[n].ti_Tag=TMOP_LeftEdge;
  187.                                     dock[n++].ti_Data=NULL;
  188.  
  189.                                     dock[n].ti_Tag=TMOP_TopEdge;
  190.                                     dock[n++].ti_Data=NULL;
  191.  
  192.                                     dock[n].ti_Tag=TMOP_Activated;
  193.                                     dock[n++].ti_Data=TRUE;
  194.  
  195.                                     dock[n].ti_Tag=TMOP_Centered;
  196.                                     dock[n++].ti_Data=FALSE;
  197.  
  198.                                     dock[n].ti_Tag=TMOP_Columns;
  199.                                     dock[n++].ti_Data=col;
  200.  
  201.                                     dock[n].ti_Tag=TMOP_Vertical;
  202.                                     dock[n++].ti_Data=orien;
  203.  
  204.                                     dock[n].ti_Tag=TMOP_Text;
  205.                                     dock[n++].ti_Data=FALSE;
  206.  
  207.                                     for(i=oldnum;i<num;i++)
  208.                                     {
  209.                                         struct tmTool *tool;
  210.  
  211.                                         if (tool=(struct tmTool *)calloc(sizeof(struct tmTool),1))
  212.                                         {
  213.                                             if ((tool->exec=calloc(12,1))&&(tool->icon=calloc(12,1)))
  214.                                             {
  215.                                                 sprintf((char *)tool->exec,"PED_E%d",i);
  216.                                                 sprintf((char *)tool->icon,"PED_I%d",i);
  217.  
  218.                                                 dock[n].ti_Tag=TMOP_Tool;
  219.                                                 dock[n++].ti_Data=(ULONG)tool;
  220.                                             }
  221.                                         }
  222.                                     }
  223.  
  224.                                     dock[n++].ti_Tag=TAG_DONE;
  225.  
  226.                                     oldnum=num;
  227.                                 }
  228.  
  229.                                 FreeArgs(RDArgs);
  230.                             }
  231.                         }
  232.                     }
  233.  
  234.                     FreeDosObject(DOS_RDARGS,RDArgs);
  235.                 }
  236.  
  237.                 Close(file);
  238.             }
  239.         }
  240.     }
  241.  
  242.     return(dock);
  243. }
  244.  
  245.  
  246.  
  247. /*****
  248.  *
  249.  * FUNZIONE:    void HandleMsg(void *handle,struct TagItem *dock)
  250.  *
  251.  * SCOPO:    Gestione dei messaggi.
  252.  *
  253.  * RESTITUISCE: -
  254.  *
  255.  ****/
  256.  
  257. void HandleMsg(void *handle,struct TagItem *dock)
  258. {
  259.     ULONG             i;
  260.     struct APIMessage    *mess;
  261.     struct Screen        *screen;
  262.     char            *pubscrname,
  263.                  tmp[25];
  264.  
  265.  
  266.  
  267.     /***** Ottieni l'indirizzo dello schermo del ProgED *****/
  268.     msg.am_MsgType=PED_API_GET_SCREEN_ADDRESS;
  269.     SendMessageToPED(&msg);
  270.     screen=(struct Screen *)msg.am_RC;
  271.  
  272.     /***** Ottieni il nome dello schermo pubblico *****/
  273.     msg.am_MsgType=PED_API_GET_PUBSCRNAME;
  274.     SendMessageToPED(&msg);
  275.     pubscrname=(char *)msg.am_RC;
  276.  
  277.     /***** Se lo schermo e' aperto apri anche il dock *****/
  278.     if (screen)
  279.     {
  280.         for(i=0;i<NumDock;i++)
  281.         {
  282.             dock[PunDock[i]].ti_Data=(ULONG)pubscrname;
  283.             dock[PunDock[i]+1].ti_Data=(DockX[i] == ~0) ? screen->Width : DockX[i];
  284.             dock[PunDock[i]+2].ti_Data=(DockY[i] == ~0) ? screen->BarHeight+1 : DockY[i];
  285.  
  286.             sprintf(tmp,"PED_DOCK%d",i);
  287.             CreateTMObjectTagList(handle,strdup(tmp),TMOBJTYPE_DOCK,&dock[PunDock[i]]);
  288.         }
  289.     }
  290.  
  291.     /***** Loop principale *****/
  292.     while(!quitting)
  293.     {
  294.         WaitPort(MyPort);
  295.         while(mess=(struct APIMessage *)GetMsg(MyPort))
  296.         {
  297.             switch(mess->am_MsgType)
  298.             {
  299.                 /***** ProgED sta' per terminare! Terminiamo
  300.                     anche noi :-( *****/
  301.                 case    PED_API_QUIT:
  302.                     quitting=TRUE;
  303.                 break;
  304.  
  305.                 /***** ProgED sta' per chiudere il suo schermo.
  306.                     Chiudiamo la nostra finestra. *****/
  307.                 case    PED_API_HIDE:
  308.                     for(i=0;i<NumDock;i++)
  309.                     {
  310.                         sprintf(tmp,"PED_DOCK%d",i);
  311.                         DeleteTMObject(handle,tmp);
  312.                     }
  313.                 break;
  314.  
  315.                 /***** ProgED ha riaperto il suo schermo.
  316.                     Riapriamo la nostra finestra. *****/
  317.                 case    PED_API_SHOW:
  318.                     if (screen=(struct Screen *)mess->am_MsgArg[0])
  319.                     {
  320.                         for(i=0;i<NumDock;i++)
  321.                         {
  322.                             dock[PunDock[i]].ti_Data=(ULONG)pubscrname;
  323.                             dock[PunDock[i]+1].ti_Data=(DockX[i] == ~0) ? screen->Width : DockX[i];
  324.                             dock[PunDock[i]+2].ti_Data=(DockY[i] == ~0) ? screen->BarHeight+1 : DockY[i];
  325.  
  326.                             sprintf(tmp,"PED_DOCK%d",i);
  327.                             CreateTMObjectTagList(handle,strdup(tmp),TMOBJTYPE_DOCK,&dock[PunDock[i]]);
  328.                         }
  329.                     }
  330.                 break;
  331.             }
  332.  
  333.             mess->am_RC=0;
  334.  
  335.             ReplyMsg((struct Message *)mess);
  336.         }
  337.     }
  338. }
  339.  
  340.  
  341.  
  342. /*****
  343.  *
  344.  * FUNZIONE:    void main(int argc,char *argv[])
  345.  *
  346.  * SCOPO:    MAIN.
  347.  *
  348.  * RESTITUISCE: -
  349.  *
  350.  ****/
  351.  
  352. void main(int argc,char *argv[])
  353. {
  354.     struct RDArgs        *RDArgs;
  355.     ULONG             args[2]={(ULONG)"progdir:docks.prefs",0,},
  356.                  try,
  357.                  i;
  358.     void            *handle;
  359.     struct TagItem        *dock;
  360.  
  361.  
  362.  
  363.     /***** Cancella i vettori DockX e DockY *****/
  364.     for(i=0;i<NUMDOCKS;i++)    DockX[i]=DockY[i]=(UWORD)~0;
  365.  
  366.     /***** Cerca la porta del ProgED *****/
  367.     if (!(PEDPort=FindPort("PED_API")))
  368.     {
  369.         printf("Can't find ProgED API Port!\n");
  370.         exit(0);
  371.     }
  372.  
  373.     /***** Crea una porta che utilizzeremo per ricevere i msg dal ProgED *****/
  374.     if (!(MyPort=CreateMsgPort()))
  375.     {
  376.         printf("Can't create a MsgPort!\n");
  377.         exit(0);
  378.     }
  379.  
  380.     /***** Riempi il campo ac_ClientPort della struttura APIClient che
  381.         utilizzeremo per la registrazione *****/
  382.     MyClient.ac_ClientPort=MyPort;
  383.  
  384.     /***** Registriamoci come cliente presso il ProgED *****/
  385.     msg.am_MsgType=PED_API_REGISTER;
  386.     msg.am_MsgArg[0]=(ULONG)&MyClient;
  387.     SendMessageToPED(&msg);
  388.  
  389.     /***** Ciclo principale *****/
  390.     if (RDArgs=ReadArgs("CONFIG/K",args,NULL))
  391.     {
  392.             /***** Aspetta il ToolManager *****/
  393.         for (try = 20; !(handle = AllocTMHandle()) && try; try--)
  394.             Delay(10);
  395.  
  396.         if (handle)
  397.         {
  398.             if (dock=SetupDock((char *)args[0],handle))
  399.                 HandleMsg(handle,dock);
  400.             FreeTMHandle(handle);
  401.         }
  402.  
  403.         FreeArgs(RDArgs);
  404.     }
  405.  
  406.     /***** Togliamo la registrazione presso il ProgED (NON in caso di quit!)*****/
  407.     if (!quitting)
  408.     {
  409.         msg.am_MsgType=PED_API_UNREGISTER;
  410.         msg.am_MsgArg[0]=(ULONG)&MyClient;
  411.         SendMessageToPED(&msg);
  412.     }
  413.  
  414.     /***** Usciamo *****/
  415.     DeleteMsgPort(MyPort);
  416.     exit(0);
  417. }
  418.  
  419.  
  420.  
  421.